package com.yan.interview.registry.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.yan.interview.registry.schema.InterviewRegistry;
import com.yan.interview.registry.service.facade.InterviewRegistryDaoService;

@Service
public class InterviewRegistryDaoServiceMangoImpl implements InterviewRegistryDaoService{

	@Autowired
	MongoTemplate mongoTemplate;

	@Override
	public void saveInterviewRegistry(InterviewRegistry interviewRegistry) {
		if(interviewRegistry != null){
			if(interviewRegistry.getInsertTime() == null){
				interviewRegistry.setInsertTime(new Date());
			}
			if(interviewRegistry.getUpdateTime() == null){
				interviewRegistry.setUpdateTime(new Date());
			}
			mongoTemplate.save(interviewRegistry, "InterviewRegistry");
		}
	}

	@Override
	public List<InterviewRegistry> findInterviewRegistryByCondition(Map<String, Object> condition) {
		Criteria criteria = null;
		
		int pageNo = 1;
		int pageSize = 10;
		
		int conditionCount = 0;
		
		for(Iterator<Entry<String, Object>> iterator = condition.entrySet().iterator();iterator.hasNext();) {
			Entry<String, Object> entry = iterator.next();
			String key = entry.getKey();
			Object value = entry.getValue();
			
			//因为查询条件改为了and，所以当条件为空字符串的时候不向查询条件中拼写
			if(value != null && !"".equals(value.toString().trim())){
				if ("id".equals(key)
						|| "genderCode".equals(key)
						|| "phone".equals(key) 
						|| "email".equals(key) 
						|| "jobExperienceYear".equals(key)
						|| "universityType".equals(key)
						|| "educationRegistWebsite".equals(key)
						|| "educationBackground".equals(key)
						|| "firstInterviewOfficer".equals(key)
						|| "firstInterviewResult".equals(key)
						|| "validStatus".equals(key)) {
					
					if(conditionCount < 1){
						criteria = Criteria.where(key).is(value.toString());
					}else{
						criteria.and(key).is(value.toString());
					}
					
					conditionCount++;
				}else if("registDay".equals(key)
						|| "firstInterviewTime".equals(key)
						|| "graduateMonth".equals(key)
						|| "firstInterviewTime".equals(key)
						|| "firstInterviewTime".equals(key)
						){
					// 字符串大于等于
					if(conditionCount < 1){
						criteria = Criteria.where(key).gte(value.toString());
					}else{
						criteria.and(key).gte(value.toString());
					}
					conditionCount++;
				}else if("jobExperienceYear".equals(key)){
					// 数字大于等于
					try {
						int intVlue = Integer.parseInt(value.toString());
						
						if(conditionCount < 1){
							criteria = Criteria.where(key).gte(intVlue);
						}else{
							criteria.and(key).gte(intVlue);
						}
						conditionCount++;
					} catch (NumberFormatException e) {
						e.printStackTrace();
					}
				}else if ("page".equals(key)) {
					pageNo = Integer.parseInt(value.toString());
				}else if ("rows".equals(key)) {
					pageSize = Integer.parseInt(value.toString());
				}else if("major".equals(key)
						|| "university".equals(key)
						|| "userName".equals(key)){
					if(conditionCount < 1){
						criteria = Criteria.where(key).regex("" + value.toString() + ".*", "i");
					}else{
						criteria.and(key).regex("" + value.toString() + ".*", "i");
					}
					
					conditionCount++;
				}else{
					
				}
			}
		}
		
		Query query = new Query();
		query.addCriteria(criteria);
		
		// 根据updateTime降序排序
		query.with(new Sort(Sort.Direction.DESC, "updateTime"));
		
		// 分页部分设置
		long skip = 0L;
		int limit = 10;
		
		skip = (pageNo - 1) * pageSize;
		limit = pageSize;
		
		query.skip(skip);
		query.limit(limit);
		
		List<InterviewRegistry> registries = mongoTemplate.find(Query.query(criteria), InterviewRegistry.class);
		return registries;
	}

	@Override
	public Long countInterviewRegistryByCondition(Map<String, Object> condition) {
		Criteria criteria = null;
		
		int conditionCount = 0;
		
		for(Iterator<Entry<String, Object>> iterator = condition.entrySet().iterator();iterator.hasNext();) {
			Entry<String, Object> entry = iterator.next();
			String key = entry.getKey();
			Object value = entry.getValue();
			
			//因为查询条件改为了and，所以当条件为空字符串的时候不向查询条件中拼写
			if(value != null && !"".equals(value.toString().trim())){
				if ("id".equals(key)
						|| "genderCode".equals(key)
						|| "phone".equals(key) 
						|| "email".equals(key) 
						|| "jobExperienceYear".equals(key)
						|| "universityType".equals(key)
						|| "educationRegistWebsite".equals(key)
						|| "educationBackground".equals(key)
						|| "firstInterviewOfficer".equals(key)
						|| "firstInterviewResult".equals(key)
						|| "validStatus".equals(key)) {
					
					if(conditionCount < 1){
						criteria = Criteria.where(key).is(value.toString());
					}else{
						criteria.and(key).is(value.toString());
					}
					
					conditionCount++;
				}else if("registDay".equals(key)
						|| "firstInterviewTime".equals(key)
						|| "graduateMonth".equals(key)
						|| "firstInterviewTime".equals(key)
						|| "firstInterviewTime".equals(key)
						){
					// 字符串大于等于
					if(conditionCount < 1){
						criteria = Criteria.where(key).gte(value.toString());
					}else{
						criteria.and(key).gte(value.toString());
					}
					conditionCount++;
				}else if("jobExperienceYear".equals(key)){
					// 数字大于等于
					try {
						int intVlue = Integer.parseInt(value.toString());
						
						if(conditionCount < 1){
							criteria = Criteria.where(key).gte(intVlue);
						}else{
							criteria.and(key).gte(intVlue);
						}
						conditionCount++;
					} catch (NumberFormatException e) {
						e.printStackTrace();
					}
				}else if ("page".equals(key)) {
					
				}else if ("rows".equals(key)) {
					
				}else if("major".equals(key)
						|| "university".equals(key)
						|| "userName".equals(key)){
					if(conditionCount < 1){
						criteria = Criteria.where(key).regex("" + value.toString() + ".*", "i");
					}else{
						criteria.and(key).regex("" + value.toString() + ".*", "i");
					}
					
					conditionCount++;
				}else{
					
				}
			}
		}
		
		Query query = new Query();
		query.addCriteria(criteria);
		
		Long totalCount = mongoTemplate.count(query, InterviewRegistry.class);
		return totalCount;
	}

	@Override
	public void updateInterviewRegistry(InterviewRegistry interviewRegistry) {
		if(interviewRegistry != null){
			
			if(interviewRegistry.getUpdateTime() == null){
				interviewRegistry.setUpdateTime(new Date());
			}
			mongoTemplate.save(interviewRegistry, "InterviewRegistry");
		}
	}
	
}
